package cn.xie.blog.commonutil.util;

import jakarta.servlet.http.HttpServletRequest;
import lombok.experimental.UtilityClass;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.ServerHttpRequest;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author: xiaoxie
 * create: 2025-04-21 21:42
 * @BelongsProject: blog-parent
 * @BelongsPackage: cn.xie.blog.commonutil.util
 * description: ip工具类
 */
@UtilityClass
public class IPUtil {

    // 缓存本地主机地址
    private static final String LOCAL_IP_ADDRESS;

    static {
        String localIp = "127.0.0.1";
        try {
            localIp = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            // 如果获取本地主机地址失败，继续使用默认值 127.0.0.1
        }
        LOCAL_IP_ADDRESS = localIp;
    }

    public static String getIpAddress(HttpServletRequest request) {
        String ipAddress = getIpFromHeaders(request);
        if (ipAddress.equals("127.0.0.1")) {
            ipAddress = LOCAL_IP_ADDRESS; // 使用缓存的本地主机地址
        }
        return ipAddress;
    }

    public static String getGatwayIpAddress(ServerHttpRequest request) {
        HttpHeaders headers = request.getHeaders();
        String ip = headers.getFirst("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddress().getAddress().getHostAddress();
        }
        return getFirstIpFromList(ip);
    }

    private static String getIpFromHeaders(HttpServletRequest request) {
        String ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
        }
        return getFirstIpFromList(ipAddress);
    }

    private static String getFirstIpFromList(String ipList) {
        if (ipList == null || ipList.isEmpty()) {
            return null;
        }
        int commaIndex = ipList.indexOf(",");
        return commaIndex != -1 ? ipList.substring(0, commaIndex).trim() : ipList.trim();
    }
    public static String getIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            throw new RuntimeException("无法获取本机ip");
        }
    }
}